#include "fig_mogaui.h"

#include "input/moga.h"
#include "ui/ui_widget.h"
#include "input/mouse.h"
#include "util/log.h"

void	FIGMOGANavigation::AddToNaviationUI(ui::UIWidget* pWidget)
{
	if(pWidget)
	{
		m_UIs.push_back(pWidget);
		if(m_pCurrentSelectUI == NULL)
		{
			m_pCurrentSelectUI = pWidget;
			//_LOG(MSG_COMMENT, pWidget->GetName().c_str());
			//if(m_pCurrentSelectUI)
			//{
			//	m_pCurrentSelectUI->SetState(ui::UIWidget::WS_MOUSE_CLICK);		
			//}
		}
	}
	
}


		
bool	FIGMOGANavigation::OnUIKeyDownEvent(int KeyCode)
{
	
	if((int) m_UIs.size() == 0)
		return false;

	ui::UIWidget* pPreviousUI = NULL;

	if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_DOWN)
	{
		m_CurrentIndex++;
		if(m_CurrentIndex >= (int)m_UIs.size())
		{
			m_CurrentIndex = 0;
		}
	}
	else if(KeyCode == input::MOGAJoystick::KEYCODE_DPAD_UP)
	{
		m_CurrentIndex--;
		if(m_CurrentIndex < 0)
		{
			m_CurrentIndex = (int)m_UIs.size() - 1;
		}
	}
	else if(KeyCode == input::MOGAJoystick::KEYCODE_BUTTON_START ||
			KeyCode == input::MOGAJoystick::KEYCODE_BUTTON_A					
		    )
	{
		if(m_pCurrentSelectUI)
		{
			m_pCurrentSelectUI->OnMouseRelease(m_pCurrentSelectUI->GetPosition(), (int)input::Mouse::BUTTON_LEFT );
		}
		
	}
	
	pPreviousUI = m_pCurrentSelectUI;
	m_pCurrentSelectUI = m_UIs[m_CurrentIndex];

	if(pPreviousUI )
	{
		pPreviousUI->SetState(ui::UIWidget::WS_MOUSE_OFF);		
	}

	if(m_pCurrentSelectUI)
	{
		m_pCurrentSelectUI->SetState(ui::UIWidget::WS_MOUSE_CLICK);		
	}

	return false;

}


void	FIGMOGANavigation::OnMOGAUIEnter()
{
	if(m_pCurrentSelectUI)
	{
		if(input::MOGAJoystick::Instance().IsConnected())
			m_pCurrentSelectUI->SetState(ui::UIWidget::WS_MOUSE_CLICK);		
		else
			m_pCurrentSelectUI->SetState(ui::UIWidget::WS_MOUSE_OFF);	
	}
}